package com.xiaok.smartclassroom.app.gateway;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.xiaok.smartclassroom.app.gateway.common.PreFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.EnableZuulServer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Classname ServerGatewayApplication
 * @Description 网关启动类
 * @Date 2021/3/23 22:08
 * @Created by xiaok
 */
@SpringCloudApplication
@EnableZuulProxy
public class ServerGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerGatewayApplication.class,args);
    }

    @Bean
    public PreFilter PreFilter(){
        return new PreFilter();
    }

    /* 解决跨域问题 */
    @Bean
    public CorsFilter corsFilter(){
         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
         CorsConfiguration config = new CorsConfiguration();
         config.setAllowCredentials(true);
         config.addAllowedOrigin("*");
         config.addAllowedHeader("*");
         config.addAllowedMethod("*");
         config.setMaxAge(18000L);
         source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

}
